package com.itheima.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.ManagerRoleMapper;
import com.itheima.pojo.*;
import com.itheima.service.ManagerRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version V1.0
 * @author: TXT
 * @date: 2020/2/12 10:00
 * @description:
 */
@Service(interfaceClass = ManagerRoleService.class)
@Transactional
public class ManagerRoleServiceImpl implements ManagerRoleService {
    @Autowired
    private ManagerRoleMapper managerRoleMapper;

    @Override
    public List<Role> findAll() {
        return managerRoleMapper.findAll();
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Page page = PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<Role> allRole = managerRoleMapper.findAll();
        return new PageResult(page.getTotal(), allRole);
    }

    @Override
    public Map findPermissionAndMenu() {
        List<Permission> permissions = managerRoleMapper.findAllPermission();
        int id = 0;
        //查询一级菜单
        List<Menu2> menusByParentId = managerRoleMapper.getMenusByParentId(id);
        getChildrenMenu(menusByParentId);

        //List<Menu> menus = managerRoleMapper.findAllMenu();
        Map map = new HashMap();
        map.put("permissions", permissions);
        map.put("menus", menusByParentId);
        return map;
    }
    private void getChildrenMenu(List<Menu2> menus){
        if (CollectionUtil.isNotEmpty(menus)){
            for (Menu2 menu : menus) {
                List<Menu2> cmenus = managerRoleMapper.getMenusByParentId(menu.getId());
                menu.setChildren(cmenus);
                getChildrenMenu(cmenus);
            }
        }
    }
    @Override
    public Map findPermIdsAndMenuIds(Integer id) {
        Role role = managerRoleMapper.findRoleById(id);
        List<Permission> permissions = managerRoleMapper.findAllPermission();
        int pid = 0;
        //查询一级菜单
        List<Menu2> menus = managerRoleMapper.getMenusByParentId(pid);
        getChildrenMenu(menus);
        List<Integer> menuIds = managerRoleMapper.findMenuIdsByRoleId(id);
        List<Integer> PermIds = managerRoleMapper.findPermIdsByRoleId(id);

        Map map = new HashMap();
        map.put("role", role);
        map.put("permissions", permissions);
        map.put("menus", menus);
        map.put("menuIds", menuIds);
        map.put("PermIds", PermIds);
        return map;
    }
    @Override
    public void addMenuAndPerm(Role role) {
        //添加角色
        managerRoleMapper.addRole(role);
        //添加菜单
        managerRoleMapper.addMenu(role.getId(),role.getMenuIds());
        //添加权限
        managerRoleMapper.addPerm(role.getId(),role.getPermIds());
    }

    @Override
    public void editMenuAndPerm(Role role) {
        managerRoleMapper.editRole(role);
        //修改菜单角色表关系，先删除，在重新添加
        managerRoleMapper.deleteMenuByRoleId(role.getId());
        if(CollectionUtil.isNotEmpty(role.getMenuIds())){
            managerRoleMapper.addMenuIdsAndRoleId(role);
        }
        //权限与上面一样
        managerRoleMapper.deletePermByRoleId(role.getId());
        if(CollectionUtil.isNotEmpty(role.getPermIds())){
            managerRoleMapper.addPermIdsAndRoleId(role);
        }
    }

    @Override
    public boolean deleteRole(Integer id) {
        //判断角色是否被用户引用
        List<Integer> userRoleById = managerRoleMapper.findUserRoleById(id);
        if(CollectionUtil.isEmpty(userRoleById)){
            //再删除 权限关系
            managerRoleMapper.deleteMenuByRoleId(id);
            //删除 菜单关系
            managerRoleMapper.deletePermByRoleId(id);
            //删除角色
            managerRoleMapper.deletaRoleById(id);
           return true;
       }
        return false;
    }


}
